import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Demo1 {

	private Scanner input;
	private File file;
	private String companyName;
	private int phoneNumber;
	private String address;

	@Override
	public String toString() {
		return "[公司名:" + companyName + ", 电话:" + phoneNumber + ", 地址:" + address + "]";
	}

	public Demo1() {
		this.input = new Scanner(System.in);
		String fileName = "config.sys";
		String dir = this.getClass().getClassLoader().getResource("").getPath();
		this.file = new File(dir, fileName);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Demo1 demo = new Demo1();
		demo.startConfig();
	}

	public void startConfig() {
		int choice;
		do {
			System.out.print("欢迎进入ACCP管理系统：\n");
			System.out.print("1.显示系统配置信息\n");
			System.out.print("2.修改系统配置信息\n");
			System.out.print("3.退出\n");
			choice = input.nextInt();
		} while (choice < 1 || choice > 3);
		if (choice == 3) {
			System.out.print("系统退出，再见！");
		} else {
			switch (choice) {
			case 1:
				showConfig();
				break;
			case 2:
				editConfig();
				break;
			default:
				break;
			}
		}
	}

	public void showConfig() {
		if (file.exists()) {
			if (readConfig()) {
				System.out.print(this.toString());
			}
		} else {
			try {
				file.createNewFile();
			} catch (IOException e) {

				e.printStackTrace();
			}
			creatConfig();
		}
	}

	public void editConfig() {
		if (file.exists()) {
			if (readConfig()) {
				int choice;
				do {
					System.out.print("选择要修改的系统配置信息：\n");
					System.out.print("1.修改公司名称\n");
					System.out.print("2.修改联系电话\n");
					System.out.print("3.修改公司地址\n");
					System.out.print("4.退出\n");
					choice = input.nextInt();
				} while (choice < 1 || choice > 4);
				if (choice == 4) {
					System.out.print("系统退出，再见！");
				} else {
					switch (choice) {
					case 1:
						System.out.print("请输入新名称:\n");
						companyName = new String(input.next());
						break;
					case 2:
						System.out.print("请输入新联系电话:\n");
						phoneNumber = input.nextInt();
						break;
					case 3:
						System.out.print("请输入新地址:\n");
						address = new String(input.next());
						break;
					default:
						break;
					}
					writeConfig();
				}
			}
		} else {
			creatConfig();
		}
	}

	public void creatConfig() {
		System.out.print("系统配置信息不存在：\n");
		System.out.print("请输入公司名称:\n");
		try {
			companyName = new String(input.next().getBytes("gbk"), "utf-8");
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		System.out.print("请输入联系电话:\n");
		phoneNumber = input.nextInt();
		System.out.print("请输入公司地址:\n");
		try {
			address = new String(input.next().getBytes("gbk"), "utf-8");
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		writeConfig();
	}

	public boolean readConfig() {
		String line;
		InputStreamReader inStream = null;
		BufferedReader br = null;
		try {
			inStream = new InputStreamReader(new FileInputStream(file));
			br = new BufferedReader(inStream);
			line = br.readLine();
			if (line != null) {
				companyName = line.split("=")[1];
			}
			line = br.readLine();
			if (line != null) {
				phoneNumber = Integer.parseInt(line.split("=")[1]);
			}
			line = br.readLine();
			if (line != null) {
				address = line.split("=")[1];
				return true;
			} else {
				creatConfig();
				return false;
			}
		} catch (IOException e) {
			System.out.print("文件读取错误!");
			e.printStackTrace();
			return false;
		} finally {
			try {
				inStream.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void writeConfig() {
		String inpuString;
		OutputStreamWriter ws = null;
		BufferedWriter bw = null;
		try {
			ws = new OutputStreamWriter(new FileOutputStream(file));
			bw = new BufferedWriter(ws);
			inpuString = "company=" + companyName;
			bw.write(inpuString);
			bw.newLine();
			inpuString = "phone=" + phoneNumber;
			bw.write(inpuString);
			bw.newLine();
			inpuString = "address=" + address;
			bw.write(inpuString);
			bw.flush();
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			try {
				ws.close();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
